<template>
  <div>
    <van-nav-bar
      title="登录"
      class="nav-userdefined"
      left-arrow
      @click-left="$router.go(-1)"
    />

    <van-form @submit="onSubmit" ref="form">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
      >
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-button
            v-if="!flag"
            size="small"
            type="primary"
            class="send-sms-btn"
            native-type="button"
            @click="captchaCode"
            >发送验证码</van-button
          >
          <!-- <van-count-down :time="time" format="ss s" /> -->
          <van-count-down :time="time" v-if="flag" @finish="flag = false">
            <template #default="timeData">
              <span class="block">{{ timeData.seconds }}{{ "  s" }}</span>
            </template>
          </van-count-down>
        </template>
      </van-field>

      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit" class="login-btn"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI, getSmsAPI } from "@/api/login.js";
export default {
  data() {
    return {
      user: {
        mobile: "",
        coed: "",
      },
      userFormRules: {
        mobile: [
          { required: true, message: "手机号不可为空" },
          {
            pattern:
              /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,
            message: "请填写正确的手机号",
          },
        ],
        code: [
          { required: true, message: "验证码不可为空" },
          {
            pattern: /^\d{4,6}$/,
            message: "请填写正确的验证码",
          },
        ],
      },
      time: "5000",
      flag: false,
    };
  },
  methods: {
    async onSubmit() {
      // 发送axios的请求
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0, // 持续展示 toast
      });
      try {
        let {
          data: { data },
        } = await loginAPI(this.user);
        console.log(data);
        this.$toast.success("登录成功");
        // 将数据存储到vuex中，保存与响应
        this.$store.commit("SaveToken", data);
        // 页面的跳转，有待进一步的优化
        this.$router.go(-1);
      } catch (error) {
        console.log(error);
        if (error.request.status === 400) {
          this.$toast.fail("手机号或验证码错误");
        } else {
          this.$toast.fail("请稍后重试");
        }
      }
    },
    async captchaCode() {
      try {
        await this.$refs.form.validate("mobile");
      } catch (error) {
        return this.$toast.fail("手机号格式错误");
      }
      this.flag = true;
      try {
        await getSmsAPI(this.user.mobile);
        this.$toast.success("验证码发送成功,请注意查收");
      } catch (error) {
        if (error.request.status === 429) {
          this.$toast.fail("操作太频繁");
        } else {
          this.$toast.fail("其他的问题");
        }
      }
    },
  },
};
</script>

<style lang='less' scoped>
.iconfont {
  font-size: 37px;
}

.van-nav-bar /deep/ .van-icon {
  color: #fff;
}

.send-sms-btn {
  display: block;
  width: 152px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: none;
}

.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}

.colon {
  display: inline-block;
  margin: 0 4px;
  color: #ee0a24;
}
.block {
  display: inline-block;
  width: 152px;
  height: 46px;
  color: #666;
  font-size: 12px;
  text-align: center;
  background-color: #ededed;
}
</style>